package cn.iocoder.yudao.module.biz.service.expand.targets.partner;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.expand.targets.partner.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.targets.partner.ExpandTargetPartnerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.biz.dal.mysql.expand.targets.partner.ExpandTargetPartnerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 拓展目标_合作伙伴 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ExpandTargetPartnerServiceImpl implements ExpandTargetPartnerService {

    @Resource
    private ExpandTargetPartnerMapper expandTargetPartnerMapper;

    @Override
    public Long createExpandTargetPartner(ExpandTargetPartnerSaveReqVO createReqVO) {
        // 插入
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        createReqVO.setOwnerUserId(loginUser.getId());
        createReqVO.setDeptId(Long.parseLong(loginUser.getInfo().get("deptId")));
        ExpandTargetPartnerDO expandTargetPartner = BeanUtils.toBean(createReqVO, ExpandTargetPartnerDO.class);
        expandTargetPartnerMapper.insert(expandTargetPartner);

        // 返回
        return expandTargetPartner.getId();
    }

    @Override
    public void updateExpandTargetPartner(ExpandTargetPartnerSaveReqVO updateReqVO) {
        // 校验存在
        validateExpandTargetPartnerExists(updateReqVO.getId());
        // 更新
        ExpandTargetPartnerDO updateObj = BeanUtils.toBean(updateReqVO, ExpandTargetPartnerDO.class);
        expandTargetPartnerMapper.updateById(updateObj);
    }

    @Override
    public void deleteExpandTargetPartner(Long id) {
        // 校验存在
        validateExpandTargetPartnerExists(id);
        // 删除
        expandTargetPartnerMapper.deleteById(id);
    }

    @Override
        public void deleteExpandTargetPartnerListByIds(List<Long> ids) {
        // 删除
        expandTargetPartnerMapper.deleteByIds(ids);
        }


    private void validateExpandTargetPartnerExists(Long id) {
        if (expandTargetPartnerMapper.selectById(id) == null) {
            throw exception(BIZ_EXPAND_TARGET_PARTNER_NOT_EXISTS);
        }
    }

    @Override
    public ExpandTargetPartnerDO getExpandTargetPartner(Long id) {
        return expandTargetPartnerMapper.selectById(id);
    }

    @Override
    public PageResult<ExpandTargetPartnerDO> getExpandTargetPartnerPage(ExpandTargetPartnerPageReqVO pageReqVO) {
        return expandTargetPartnerMapper.selectPage(pageReqVO);
    }

    @Override
    public Integer checkExistByName(String name) {
        return expandTargetPartnerMapper.checkExistByName(name);
    }
}